#  Name: CC-search v0.2
#  Description: A set of plugins for the Gnome deskbar applet to search for Creative commons
#  licensed contents using http://search.creativecommons.org
#
#  Copyright (C) 2006 Jaime Frutos Morales <acidborg@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

from deskbar.Handler import Handler
from deskbar.Match import Match
from gettext import gettext as _
import gtk, gtk.glade, gconf, gnomevfs, deskbar
import os.path

CC_SEARCH_URL = "http://search.creativecommons.org/?q="
INSTALL_DIRECTORY = os.path.expanduser("~/.gnome2/deskbar-applet/handlers")
CC_ICON = INSTALL_DIRECTORY + "/cc.png"
GCONF_PATH  = deskbar.GCONF_DIR + "/cc-search"

def _on_button_toggled(widget, data=None):
	if data == 1:
		if widget.get_active() == True:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/google",1)
		else:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/google",0)
	elif data == 2:
		if widget.get_active() == True:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/yahoo",1)
		else:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/yahoo",0)
	elif data == 3:
		if widget.get_active() == True:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/flickr",1)
		else:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/flickr",0)
	elif data == 4:
		if widget.get_active() == True:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/comm",1)
		else:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/comm",0)
	elif data == 5:
		if widget.get_active() == True:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/deriv",1)
		else:
			deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/deriv",0)


def _set_preferences(dialog):
	glade = gtk.glade.XML(os.path.join(INSTALL_DIRECTORY, "cc-search.glade"))
	dialog = glade.get_widget("preferences")
	check_google = glade.get_widget("check_google")
	check_google.set_active(deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/google"))
	check_yahoo = glade.get_widget("check_yahoo")
	check_yahoo.set_active(deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/yahoo"))
	check_flickr = glade.get_widget("check_flickr")
	check_flickr.set_active(deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/flickr"))
	check_comm = glade.get_widget("check_comm")
	check_comm.set_active(deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/comm"))
	check_deriv = glade.get_widget("check_deriv")
	check_deriv.set_active(deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/deriv"))

	check_google.connect("toggled", _on_button_toggled, 1)
	check_yahoo.connect("toggled", _on_button_toggled, 2)
	check_flickr.connect("toggled", _on_button_toggled, 3)
	check_comm.connect("toggled", _on_button_toggled, 4)
	check_deriv.connect("toggled", _on_button_toggled, 5)
	
	dialog.set_icon(deskbar.Utils.load_icon(CC_ICON))
	dialog.show_all()
	dialog.run()
	dialog.destroy()


def _check_gconf_keys():
	# Check gconf keys and generate them if necessary
	if not deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/google"):
		deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/google",0)

	if not deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/yahoo"):
		deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/yahoo",0)

	if not deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/flickr"):
		deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/flickr",0)

	if not deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/comm"):
		deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/comm",0)

	if not deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/deriv"):
		deskbar.GCONF_CLIENT.set_int(GCONF_PATH + "/deriv",0)


def _check_requirements():
	# Create user preferences if they don't exist
	_check_gconf_keys()
	return (deskbar.Handler.HANDLER_IS_CONFIGURABLE, _("You can set your CC Search preferences."), _set_preferences)

HANDLERS = {
	"CC_SearchHandler" : {
		"name": _("CC Search"),
		"description": _("Search for CC licensed content"),
		"requirements" : _check_requirements,
		"version": 0.2,
	}
}


class CC_SearchMatch(Match):
	def __init__(self, backend, **args):
		Match.__init__(self, backend, **args)

	def action(self, text=None):
		# Check engine, comm and deriv options
		if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/google") == 1:
			rights = ""
			commercial = False
			if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/comm") ==0: 
				rights += "|cc_noncommercial"
				commercial = True
			if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/deriv") ==0: 
				rights += "|cc_nonderived";

			rights += ")";
			gnomevfs.url_show("http://google.com/search?q=" + self.name + "&as_rights=(cc_publicdomain|cc_attribute|cc_sharealike" + rights)

		if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/yahoo") == 1:
			rights="&"
			if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/comm") ==1: 
				rights+="ccs=c&"
			if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/deriv") ==1: 
				rights+="ccs=e"
			if rights== "&":
				rights=""
			gnomevfs.url_show("http://search.yahoo.com/search?cc=1&p=" + self.name + rights)

		if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/flickr") == 1:
			rights="&l="
			if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/comm") ==1: 
				rights+="comm"
			if deskbar.GCONF_CLIENT.get_int(GCONF_PATH + "/deriv") ==1: 
				rights+="deriv"
			if rights == "&l=":
				rights+="cc"
			gnomevfs.url_show("http://flickr.com/search/?q=" + self.name + rights)

	def get_category(self):
		return "web"

	def get_verb(self):
		return _("Search for CC licensed content with <b>%(name)s</b>")

	def get_hash(self, text=None):
		return self.name


class CC_SearchHandler(Handler):
	def __init__(self):
		Handler.__init__(self, CC_ICON)

	def query(self, query):
		query = query.lower()
		return [CC_SearchMatch(self, name=query)]
